﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using FrameWorkCore;
using FrameWorkCore.EFCore;
using FrameWorkCore.HangFire;
using Hangfire;
using ShardingCore.Bootstrapers;

namespace WebApi6
{
    public static class Pipeline
    {
        
        /// <summary>
        /// 管道处理模型
        /// </summary>
        /// <param name="builder"></param>
        public static void PipelineRun(this WebApplicationBuilder builder)
        {

            builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory(x => x.RegisterModule(new AutoFacModule())));

            var app = builder.Build();

      
            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
            //    app.UseDeveloperExceptionPage();

            //app.UseResponseCaching();

            app.UseHangFireRun(); 

            app.UseResponseCompression();

            //app.UseShardingCore(); 不在这里start 迁移会报错

            app.UseStaticFiles();

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                #if DEBUG
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "BearerAuth");
                #else
                c.SwaggerEndpoint($"{builder.Configuration["AppAddressUrl"]}/swagger/v1/swagger.json", "BearerAuth");
                #endif
                c.IndexStream = () => new MemoryStream(File.ReadAllBytes(Path.Combine(builder.Environment.WebRootPath, "swagger/ui/index.html")));

            });

            app.UseCors("AllowAnonymous");

            app.UseHttpsRedirection();

            app.UseAuthentication();

            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }

    }
}
